//
//  DataManager.m
//  DataManager
//
//  Created by Michael Hinds on 2/25/09.
//  Copyright 2009 University of Arkansas. All rights reserved.
//

#import "DataManager.h"


@implementation DataManager
@synthesize parser, remoteKey, userID;

/***************************CONSTRUCTOR********************************/
-(id) init
{
	return self;
}

-(id) initWithRemoteKey:(NSString *) rKey AndUserID:(NSInteger) uID
{
	remoteKey = rKey;
	userID = uID;	
	
	return self;
}

/*****************************Getters & Setters************************/
//since the @property directives are used on the remote key and userID, then 
//we don't need getters and setters for them. They are automatically included.

/***************************API METHODS********************************/
//Get a user's Profile
-(NSDictionary *) getProfile
{
	
	//instantiate parser if not already done
	if(parser == NULL)
	{
		parser = [[SBJSON alloc] init]; 
	}
	
	//Debug
	NSLog(@"Entered getProfile method");
	
	NSString* fullURL = [NSString stringWithFormat:@"http://www.kaching.com/api/users/%d/portfolio", userID];
		
	NSLog(@"Created URL: %@", fullURL);
	
	// Prepare URL request to download Profile from KaChing
	NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:fullURL]];
	
	// Perform request and get JSON back as a NSData object
	NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
	
	// Get JSON as a NSString from NSData response
	NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
	//txtArea.text = [txtArea.text stringByAppendingString: json_string];
	
	NSLog(json_string);
	
	// parse the JSON response into an object
	NSDictionary *profile = [parser objectWithString:json_string error:nil];
	
	return profile;

	[request release];
	[response release];
	[json_string release];
	[profile release];
}


@end
